<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<style type="text/css">
#supplier_annex .pop-ups p {
	margin: 0;
	text-align: right;
}

#supplier_annex .pop-ups .uploadFile {
	width: 64px;
}

#supplier_annex .pop-ups table {
	width: 100%;
	border: 1px solid #dcdcdc;
	box-sizing: border-box;
}

#supplier_annex .pop-ups td a {
	margin: 0 10px;
}

#supplier_annex input[type="file"] {
    display: none;
}
</style>
<div class="modal fade" id="supplier_annex" tabindex="-1" role="dialog"
	aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document" style="width: 640px;">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title" id="myModalLabel">附件</h4>
			</div>
			<div class="modal-body box pop-ups">
				<div>
					<form id="uploadSupplierAnnexForm" enctype="multipart" class="form">
						<p>
							<c:if test="${isUploadAnnex }">
								<a class="uploadFile"><label for="protocoenclosurename">上传文件</label> <input type="file"
									name="protocoenclosurename" id="protocoenclosurename"
									onchange="uploadAnnex();"></input>
								</a>
							</c:if>
							<input type="hidden" id="supplierid" name="supplierid" />
						</p>
					</form>
					<table  class="table table-bordered table_text_center">
						<thead>
							<tr>
								<th>序号</th>
								<th>附件</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody id="supplierAnnexList">
							<!-- 			<tr>
							<td>1</td>
							<td>组织机构代码证.docx</td>
							<td><a href="#">下载</a><a href="#">删除</a></td>
						</tr> -->
						</tbody>
					</table>
				</div>
			</div>
			<div class="red" id="add_supplier_msg" style="text-align: center;"></div>
			<div class="modal-footer">
				<c:if test="${isDownloadAnnex }">
					<input type="hidden" id="isDownloadAnnex">
				</c:if>
				<c:if test="${isDeleteAnnex }">
					<input type="hidden" id="isDeleteAnnex">
				</c:if>
				<button type="button" class="btn btn2" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>
<script>

function showSupplierAnnex(supplierid){
	$('#supplier_annex #supplierid').val(supplierid);
	listAnnex(supplierid);
	$('#supplier_annex').modal('show');
}

function listAnnex(supplierid) {
	$.ajax({
		url:"<%=basePath%>supplierannex/annexlist.do",
		type:"post",
		async : false,
		data:"supplierid="+supplierid,
		dataType:"json",
		success:function(data){
			$("#supplierAnnexList").html("");
			var list = data;
			for (var i = 0; i < list.length; i++) {
				var html="<tr id='"+list[i].supplierannexid+"' align='center'><td>"+(i+1)+"</td>"
				+"<td>"+ list[i].annexname+ "</td>"
				+"<td>";
				if($("#isDownloadAnnex").length>0)
					html+="<a onclick='downloadAnnex("+list[i].supplierannexid+",\""+list[i].annexpath+"\")'>下载</a>";
				if($("#isDeleteAnnex").length>0)
					html+="<a onclick='deleteAnnex("+list[i].supplierannexid+",\""+list[i].annexpath+"\")'>删除</a>";
				html+="</td></tr>";
				$("#supplierAnnexList").append(html);
			}
		}
	});
}

function downloadAnnex(id,path){
	window.open("<%=basePath%>supplierannex/download.do?supplierannexid="+id+"&path="+path);
}

function deleteAnnex(id,path) { 
	$.ajax({  
		url: '<%=basePath%>supplierannex/delete.do' ,  
		type: 'POST',  
		async : false,
		data: {supplierannexid:id,path:path}, 
		dataType:"json",
		success: function (data) {  
			Prompt.alert("提示","文件已删除!")
			listAnnex($("#supplier_annex #supplierid").val());
       } 
    });
}

function uploadAnnex() {  
	if($("#supplier_annex #protocoenclosurename" ).val()==''){
		Prompt.alert("提示","请选择文件!")
		return;
	}
    var formData = new FormData($("#uploadSupplierAnnexForm")[0]);  
    $.ajax({  
         url: '<%=basePath%>supplierannex/add.do',
		type : 'POST',
		data : formData,
		async : false,
		cache : false,
		dataType : "json",
		contentType : false,
		processData : false,
		success : function(data) {
			Prompt.alert("提示", "文件已上传!")
			listAnnex($("#supplier_annex #supplierid").val());
		},
		error : function(returndata) {
			alert("文件上传失败！");
		}
	});
	$("#supplier_annex #protocoenclosurename").val("");
}
</script>